package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctQualityControlBo;
import com.rzdata.poct.system.domain.business.po.PoctQualityControl;
import com.rzdata.poct.system.domain.business.vo.PoctQualityControlVo;
import com.rzdata.poct.system.mapper.PoctQualityControlMapper;
import com.rzdata.poct.system.service.IPoctQualityControlService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * poct 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctQualityControlServiceImpl extends ServicePlusImpl<PoctQualityControlMapper, PoctQualityControl, PoctQualityControlVo> implements IPoctQualityControlService {

    @Override
    public PoctQualityControlVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctQualityControlVo> queryPageList(PoctQualityControlBo bo) {
        PagePlus<PoctQualityControl, PoctQualityControlVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctQualityControlVo> queryList(PoctQualityControlBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctQualityControl> buildQueryWrapper(PoctQualityControlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctQualityControl> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQualityNo() != null, PoctQualityControl::getQualityNo, bo.getQualityNo());
        lqw.eq(bo.getProjectId() != null, PoctQualityControl::getProjectId, bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), PoctQualityControl::getProjectName, bo.getProjectName());
        lqw.eq(bo.getInstrumentId() != null, PoctQualityControl::getInstrumentId, bo.getInstrumentId());
        lqw.eq(StringUtils.isNotBlank(bo.getInstrumentNo()), PoctQualityControl::getInstrumentNo, bo.getInstrumentNo());
        lqw.eq(bo.getType() != null, PoctQualityControl::getType, bo.getType());
        lqw.eq(bo.getDeptId() != null, PoctQualityControl::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), PoctQualityControl::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getInspector()), PoctQualityControl::getInspector, bo.getInspector());
        lqw.eq(bo.getQualityStatus() != null, PoctQualityControl::getQualityStatus, bo.getQualityStatus());
        lqw.eq(bo.getQualityValue() != null, PoctQualityControl::getQualityValue, bo.getQualityValue());
        lqw.eq(bo.getQualitySdvalue() != null, PoctQualityControl::getQualitySdvalue, bo.getQualitySdvalue());
        lqw.eq(bo.getPublishTime() != null, PoctQualityControl::getPublishTime, bo.getPublishTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctQualityControlBo bo) {
        PoctQualityControl add = BeanUtil.toBean(bo, PoctQualityControl.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctQualityControlBo bo) {
        PoctQualityControl update = BeanUtil.toBean(bo, PoctQualityControl.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctQualityControl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
